import { _modify } from '../../common/api/signin'
const app = getApp()
Page({
  data: {
    old: '',
    newv: '',
    newv1: ''
  },
  inputHandler(e) {
    let attr = e.currentTarget.dataset.attr
    this.data[attr] = e.detail
  },
  submit () {
    if (!this.data.old.trim()) {
      app.toast('请输入旧密码')
      return
    }
    if (!this.data.newv.trim()) {
      app.toast('请输入新密码')
      return
    }
    if (this.data.newv1 !== this.data.newv) {
      app.toast('两次输入的新密码不一致')
      return
    }
    let id = wx.getStorageSync('memberid')
    app.loading('加载中')
    _modify(id, this.data.old, this.data.newv).then(res => {
      console.log(res)
      wx.hideLoading()
      if (res.data.IsSuccess) {
        wx.showModal({
          title: '温馨提示',
          content: '修改成功',
          showCancel: false,
          success: r => {
            if (r.confirm) {
              wx.removeStorage({
                key: 'memberid',
                success: res => {
                  wx.redirectTo({
                    url: '/pages/signin/index'
                  })
                }
              })
            }
          }
        })
      } else {
        wx.showModal({
          title: '对不起',
          content: res.data.Msg,
          showCancel: false
        })
      }
    }).catch(err => {
      console.log(err)
      wx.hideLoading()
      wx.showModal({
        title: '对不起',
        content: '请求失败，请稍后再试',
        showCancel: false
      })
    })
  },
  onLoad (options) {},
  onReady () {},
  onShow () {
    // if (app.store.userinfo) {
    //   this.data.userinfo = app.store.userinfo
    // } else {
    //   app.loading('加载中')
    //   app.launch().then(res => {
    //     wx.hideLoading()
    //     console.log(res)
    //     this.data.userinfo = res
    //   }).catch(err => {
    //     wx.hideLoading()
    //     console.log(err)
    //   })
    // }
  },
  onHide () {},
  onUnload () {},
  onPullDownRefresh () {},
  onReachBottom () {},
  onShareAppMessage () {}
})